/*
 * Copyright (C) 2006 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.common.action;

import java.awt.event.ActionEvent;

import nu.zoom.swing.desktop.WorkbenchFrame;
import nu.zoom.swing.desktop.WorkbenchFrameListener;

/**
 * An action that detaches a frame from the desktop area and promotes it to a
 * top-level frame and back again. This is the action that is used on the window
 * menu to move frames in and out of the desktop area.
 */
public abstract class DetachAction extends WorkbenchAction implements WorkbenchFrameListener {
	private static final long serialVersionUID = -1885789576367812373L;
	private final WorkbenchFrame frame;

	/**
	 * Create a detach/attach action.
	 * 
	 * @param frame
	 */
	public DetachAction(WorkbenchFrame frame) {
		super();
		this.frame = frame;
		if (frame.isDetached()) {
			setDetachedLook();
		} else {
			setAttachedLook();
		}
		frame.addFrameListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (frame.isDetached()) {
			frame.attach();
		} else {
			frame.detach();
		}
	}

	@Override
	public void frameClosed(WorkbenchFrame frame) {
	}

	@Override
	public void frameWillDispose(WorkbenchFrame frame) {
		frame.removeFrameListener(this);
	}

	@Override
	public void frameDetached(WorkbenchFrame frame) {
		setDetachedLook();
	}

	@Override
	public void frameAttached(WorkbenchFrame frame) {
		setAttachedLook();
	}

	protected abstract void setAttachedLook();

	protected abstract void setDetachedLook();
}
